const Controller = require("egg").Controller;

class DeviceController extends Controller {

  //列表
  async index() {

    const { ctx, service } = this

    const { page=1, pageSize=10 } = ctx.query

    const payload = {
      page,
      pageSize
    }

    console.log(payload, 'device.js')

    const result = await service.deviceService.getSourceData(payload)

    ctx.helper.success({ ctx, res: result })
  }

  //添加设备
  async create() {

    const { ctx, service } = this

    const payload = ctx.request.body

    payload.pdi_updatetime = ctx.helper.formatTime()

    await service.deviceService.add(payload)

    ctx.helper.success({ ctx })
  }

  //显示设备详细信息
  async show() {

    const { id } = ctx.params

    return await service.deviceService.show(id)
  }

  //更新设备信息
  async update() {

    const { ctx, service } = this

    const { id } = ctx.params

    const payload = ctx.request.body

    payload.pdi_updatetime = ctx.helper.formatTime()

    await service.deviceService.update(id, payload)

    ctx.helper.success({ ctx })
  }

  //删除设备
  async destroy() {

    const { ctx, service } = this

    const { id } = ctx.params

    await service.deviceService.destroy(id)

    ctx.helper.success({ ctx })

  }
}

module.exports = DeviceController;
